package com.xiaoyy.app.extend.gateway.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.xiaoyy.app.extend.common.enums.AppApiEnum;
import com.xiaoyy.app.extend.gateway.req.BindUserWinxinReq;
import com.xiaoyy.app.extend.gateway.req.LoginWinxinReq;
import com.xiaoyy.app.extend.util.WechatUtil;
import com.xiaoyy.core.common.exception.AppException;
import com.xiaoyy.core.common.vo.AppResponse;
import com.xiaoyy.core.common.vo.TokenVO;
import com.xiaoyy.app.extend.gateway.validators.LoginValidators;
import com.xiaoyy.core.common.enums.LoginTypeEnum;
import com.xiaoyy.core.common.vo.LoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;

/**
 * 就业创业码用户登入相关
 *
 * @author chenzh
 * @date 2018-08-17
 */
@RestController
@RequestMapping(value = "/other/sso/jycy")
@Api(tags = "第三方登录管理")
public class LoginController {
    @Value("${system.config.app.url}")
    private String appUrl;
    /**
     * 用户登入
     */
    @PostMapping(value = "/login/weixin", produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "微信登录", notes = "用户登录")
    public AppResponse<TokenVO> loginWinxin(@RequestBody @Validated(value = {LoginValidators.ToLogin.class}) LoginWinxinReq loginReq, @ApiIgnore HttpServletRequest request) {
        if (StringUtils.isBlank(loginReq.getLoginKey())) {
            if (request.getSession() != null) {
                //session 不为空为PC端
                loginReq.setLoginKey(request.getSession().getId());
            }
        }
        LoginVO loginVO = Convert.convert(LoginVO.class, loginReq);
        if (StringUtils.isBlank(loginVO.getOpenId())) {
            loginVO.setOpenId(getWinxinOpenId(loginReq));
        }
        loginVO.setLoginType(LoginTypeEnum.weixin.getType());
        try {
            String body = HttpUtil.post(appUrl + AppApiEnum.LOGIN0001.getType(), JSON.toJSONString(loginVO));
            return JSON.parseObject(body, AppResponse.class);
        }catch (Exception e){
            return AppResponse.errorMsg("内部服务请求异常");
        }
    }
    /**
     * 微信绑定用户
     */
    @PostMapping(value = "/bindUser/weixin", produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "微信绑定用户", notes = "微信绑定用户")
    public AppResponse<TokenVO> bindWinxinUser(@RequestBody BindUserWinxinReq bindUserWinxinReq) {
        LoginWinxinReq loginWinxinReq= Convert.convert(LoginWinxinReq.class, bindUserWinxinReq);
        if (StringUtils.isBlank(bindUserWinxinReq.getOpenId())){
            bindUserWinxinReq.setOpenId(getWinxinOpenId(loginWinxinReq));
        }
        try{
            String body= HttpUtil.post(appUrl+ AppApiEnum.LOGIN0002.getType(), JSON.toJSONString(bindUserWinxinReq));
            return JSON.parseObject(body,AppResponse.class);
        }catch (Exception e){
            return AppResponse.errorMsg("内部服务请求异常");
        }
    }

    /**
     * 获取微信openId
     * */
    private String getWinxinOpenId(LoginWinxinReq loginReq){
        String openId = loginReq.getOpenId();
        if (StringUtils.isBlank(openId)){
            openId = WechatUtil.getSessionKeyOrOpenId(loginReq.getCode()).getOpenId();
        }
        if (StringUtils.isBlank(openId)){
            throw new AppException("未获取到openid");
        }
        return openId;
    }

}
